//#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include<conio.h>

 IplImage* frame=0;
IplImage* imgTracking;
int lastX = -1;
int lastY = -1;
 int a,b,c,d;
int a1,b1,c1,d1;

 IplImage* GetThresholdedImage(IplImage* imgHSV){
    IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
    cvInRangeS(imgHSV, cvScalar(a,b,c,d), cvScalar(a1,b1,c1,d1), imgThresh);
    return imgThresh;
    }




//red
void trackObject(IplImage* imgThresh){
// Calculate the moments of 'imgThresh'
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
cvMoments(imgThresh, moments, 1);
double moment10 = cvGetSpatialMoment(moments, 1, 0);
double moment01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);

     // if the area<1000, I consider that the there are no object in the image and it's because of the noise, the area is not zero
if(area>1000){
        // calculate the position of the ball
int posX = moment10/area;
int posY = moment01/area;

       if(lastX>=0 && lastY>=0 && posX>=0 && posY>=0)
{
// Draw a yellow line from the previous point to the current point
cvLine(imgTracking, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,0,255,0), 4,8,0);
}

lastX = posX;
lastY = posY;
}

free(moments);
}



int main(){

      CvCapture* capture =0;
<<<<<<< .mine
      int i;
    char Data_A='A';
    char Data_B='B';

      capture = cvCaptureFromCAM(0);
      if(!capture){
            printf("Capture failure\n");
            return -1;
      }

      IplImage* frame=0;
      cvNamedWindow("Video",0);
      cvNamedWindow("Ball",0);


//wait for input character 'P' from user
    while(1)
    {
              //Wait 50mS
            int c1 = cvWaitKey(10);
            //If 'ESC' is pressed, break the loop
            if((char)c1=='P' ) break;
      }
=======
      int i;
      int height,width,depth,step,channels,j,k;
      uchar* data;
      int tran=1, tran1=2, tran2=3,tran3=4;
      char rec,ar=1;
      char ar1='1';
      char ar2='2';
      char ar3='3';
      char ar4='4';
      capture = cvCaptureFromCAM(1);
>>>>>>> .r25

      if(!capture){
printf("Capture failure\n");
return -1;
      }


      frame = cvQueryFrame(capture);
      if(!frame) return -1;

     //create a blank image and assigned to 'imgTracking' which has the same size of original video
     imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3);
     cvZero(imgTracking); //covert the image, 'imgTracking' to black

     cvNamedWindow("Video",1);
     cvNamedWindow("output",1);



//if(Send_Serial_Data(&Data_A))
      //iterate through each frames of the video
      while(1){

            frame = cvQueryFrame(capture);
            if(!frame) break;
            frame=cvCloneImage(frame);

           cvSmooth(frame, frame, CV_GAUSSIAN,3,3,0,0); //smooth the original image using Gaussian kernel

             IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
            cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV


            IplImage* imgThresh = GetThresholdedImage(imgHSV);

            cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3,0,0); //smooth the binary image using Gaussian kernel

          //track the possition of the ball
          trackObject(imgThresh);

            // Add the tracking image and the frame
          cvAdd(frame, imgTracking, frame,0);

           cvShowImage("output",imgThresh);
           cvShowImage("video",frame);



rec=read_bytes(&ar);
printf("rec  = %c",rec);
if(rec=='A')
{
a=170;b=160;c=60;d=0;
    a1=180;b1=255;c1=256;d1=0;
    tran=Send_Serial_Data(&ar1);
    printf("tran =%c",tran);

}
else if(rec=='B')
{
    a=22;b=49;c=120;d=0;
    a1=32;b1=255;c1=256;d1=0;
     tran1=Send_Serial_Data(&ar2);
    printf("tran1 =%c",tran1);
}
/*else if(rec=='C')
{
    a=101;b=167;c=136;d=;
    a1=116;b1=256;c1=256;d1=0;
}
else
{
    a=42;b=69;c=57;d=1;
    a1=75;b1=255;c1=256;d1=1;
}*/



printf("end \n");


           cvReleaseImage(&imgHSV);
           cvReleaseImage(&imgThresh);
           cvReleaseImage(&frame);

            //Wait 10mS
            int c = cvWaitKey(10);
            //If 'ESC' is pressed, break the loop
            if((char)c==27 ) break;
      //}
      }
      cvDestroyAllWindows() ;
      cvReleaseImage(&imgTracking);
      cvReleaseCapture(&capture);

      return 0;
}







